package com.example.demo.web.filter;

import com.example.demo.util.RepeatedlyRequestWrapper;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 由于 ServletRequest 中的流数据只能读取一次，必须最开始就得提前缓存
 *
 * @author hao.chen
 * @date 2023/11/6 19:55
 */
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class RepeatableFilter extends OncePerRequestFilter {


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        ServletRequest requestWrapper = new RepeatedlyRequestWrapper(request);
        filterChain.doFilter(requestWrapper, response);
    }
}
